package com.example.xunwuproject;

import com.google.common.collect.Sets;
import org.springframework.data.domain.Sort;

import java.util.Set;

/**
 * @author ：Anitaxiaofeng
 * @description：
 * @create 2019-03-13
 **/
public class HouseSort {

    public static final String DEFAULT_SORT_KEY = "lastUpdateTime";
    public static final String DISTANCE_TO_SUBWAY_KEY = "distanceToSubway";

    private static final Set<String> SORT_KEYS = Sets.newHashSet(
            DEFAULT_SORT_KEY,
            DISTANCE_TO_SUBWAY_KEY,
            "createTime",
            "price",
            "area"
    );
    public static Sort generateSort(String key, String directionKey) {
        key = getSortKey(key);
        Sort.Direction direction = Sort.Direction.fromStringOrNull(directionKey);
        if(direction == null) {
            direction = Sort.Direction.DESC;
        }
        return new Sort(direction,key);
    }

    public static String getSortKey(String key) {
        if(!SORT_KEYS.contains(key)) {
            return DEFAULT_SORT_KEY;
        }
        return key;
    }

}
